
package privatemessanger;
import javax.swing.*;
import java.awt.*;


/**
 * This panel contain a text field for incoming messages.
 * @author  Erez Fatal, Oren Slor, Elad Yanai, Sigal Agami, Chen Ben Shabat,
 *          Inbar Pardo, Islamm Heralla, Ziv Hadary, Oren Shalev, Ortal David.
 */
public class MessagePanel extends JPanel
{
    private MainFrame _frame; // the frame.
    private JTextArea _textArea; // the text area.
    private ImageIcon _imageIcon; // the background.

    /**
     * This is the MessagePanel constructor.
     * @param frame The frame which creates this panel.
     */
    MessagePanel(MainFrame frame)
    {
        _frame=frame;
        setBackground(Color.LIGHT_GRAY);
        _imageIcon=new ImageIcon("src/privatemessanger/background.jpg");
        _textArea=new JTextArea(24, 33)
        {
            Image image = _imageIcon.getImage();
            {
                setOpaque(false);
            }

            @Override
            public void paint(Graphics g)
            {
                for (int i=0; i<=53000; i+=530)
                        g.drawImage(image, -80, i, this);
                super.paint(g);
            }
        };
        _textArea.setLineWrap(true);
        _textArea.setEditable(false);
        _textArea.setFont(new Font("Arial", Font.BOLD, 18));
        JScrollPane scrollPane = new JScrollPane(_textArea);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        add(scrollPane);
    }

    /**
     * This method cleans the text from the text area.
     */
    public void cleanText() {_textArea.setText(""); }

    /**
     * This method add a message to the text area.
     * @param message A new message.
     */
    public void addText(Message message)
    {
        if (_textArea.getText().equals(""))
            _textArea.setText("("+message.getTime()+")"+" "+message.getSenderName()+" says to "+message.getReceiverName()+" :\n"+message.getMessage());
        else
            _textArea.setText(_textArea.getText()+"\n\n"+"("+message.getTime()+")"+" "+message.getSenderName()+" says to "+message.getReceiverName()+" :\n"+message.getMessage());
    }
}
